package com.lagou.controller;

import com.lagou.mapper.UserMapper;
import com.lagou.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserMapper mapper;

    @RequestMapping("toLogin")
    public String toLogin() {
        System.out.println("================++++++++++++++跳转登录页面");
        return "login";
    }

    @RequestMapping("loginSystem")
    public String loginSystem(String username, String password, HttpSession session) {
        UserInfo userInfo = mapper.findUserInfo(username);
        session.setAttribute("username",null);
        session.setAttribute("password",null);
        if(userInfo == null){
            // 非法用户返回登录页面
            System.out.println("用户不存在");
            return "redirect:/login/toLogin";
        }
        // 合法用户，信息写入session，同时跳转到系统主页面
        if(userInfo.getPassword().equals(password)) {
            System.out.println("合法用户");
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            return "redirect:/demo/result";
        }else{
            // 非法用户返回登录页面
            System.out.println("密码不正确");
            return "redirect:/login/toLogin";
        }
    }
}
